package me.zhengjie.modules.test.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.test.domain.JgxTest;
import me.zhengjie.modules.test.service.JgxTestService;
import me.zhengjie.modules.test.service.dto.JgxTestQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
* @author jie
* @date 2019-06-25
*/
@RestController
@RequestMapping("api")
public class JgxTestController {

    @Autowired
    private JgxTestService jgxTestService;

    @Log("查询JgxTest")
    @GetMapping(value = "/jgxTest")
    @PreAuthorize("hasAnyRole('ADMIN','JGXTEST_ALL','JGXTEST_SELECT')")
    public ResponseEntity getJgxTests(JgxTestQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity(jgxTestService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("新增JgxTest")
    @PostMapping(value = "/jgxTest")
    @PreAuthorize("hasAnyRole('ADMIN','JGXTEST_ALL','JGXTEST_CREATE')")
    public ResponseEntity create(@Validated @RequestBody JgxTest resources){
        return new ResponseEntity(jgxTestService.create(resources),HttpStatus.CREATED);
    }

    @Log("修改JgxTest")
    @PutMapping(value = "/jgxTest")
    @PreAuthorize("hasAnyRole('ADMIN','JGXTEST_ALL','JGXTEST_EDIT')")
    public ResponseEntity update(@Validated @RequestBody JgxTest resources){
        jgxTestService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("删除JgxTest")
    @DeleteMapping(value = "/jgxTest/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','JGXTEST_ALL','JGXTEST_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id){
        jgxTestService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}